<template>
    <div>   
    <el-dialog
        title="增加图书"
        v-model="dialogFormVisible"
        :before-close="clear"
        width=30%
        >  
        <el-form :model="form" :rules="rules" style="text-align:left" ref="form" >
        <el-form-item label="图书名" :label-width="formLabelWidth" prop="bookName">
            <el-input type="text" name="bookName" v-model="form.bookName" autocomplete="off" placeholder="请输入图书名" ></el-input>
        </el-form-item>
  
        <el-form-item label="图书类型" :label-width="formLabelWidth" prop="bookType">
          <el-select v-model="form.bookType" placeholder="请选择图书类型">
            <el-option
              v-for="type in bookTypes"
              :key="type.bookType"
              :label="type.bookType"
              :value="type.bookType">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="作者名" :label-width="formLabelWidth" prop="author">
            <el-input type="text" name="author" v-model="form.author" autocomplete="off" placeholder="请输入作者名" ></el-input>
        </el-form-item> 
        </el-form>
        
        <div slot="footer" class="dialog-footer" align="right">
          <el-button @click="dialogFormVisible = false">取 消</el-button>       
          <el-button type="primary" @click="onSubmit()">确 定</el-button>
        </div>         
      </el-dialog>     
    </div>
  </template>
  <script>
  export default {
      name: 'AddBook',
      components:{},
      data () {
        return {
          dialogFormVisible: false,        
          form: {                             
             bookName: '',         
             bookType: '',         
             isBorrowed: false,         
             author: '',         
          },
          formLabelWidth: '80px',
          rules: {            
            bookName: [
              { required: true, message: '请输入图书名', trigger: 'blur' },
            ],
            bookType: [
              { required: true,  message: '请选择图书类型', trigger: 'change' },
            ],
            author: [
              { required: true, message: '请输入作者名',trigger: 'blur' },
            ],
          },
          bookTypes: [],
        }
      },
      methods: {
        clear () {       
          this.form = {            
            bookName:'',
            bookType:'', 
            author:'',                             
          }
        },     
        getAllBookTypes(){
          this.$api.post('bookType/getAllBookType', null).then((res) => {                        
            if (res.code === 200) {     
            this.bookTypes = res.data.sort((a, b) => a.id - b.id);
        } else {
          alert('获取图书类型数据失败!')
        }
        })    
        },
        onSubmit () {        
            if((this.form.bookName.trim() !== '' 
                && this.form.bookName !== undefined) 
                && (this.form.bookType !== '' 
                && this.form.bookType !== undefined
                && this.form.author.trim() !== '' 
                && this.form.author !== undefined))
            {   
              //alert("用户账号:"+ this.form.username ) 
              this.$confirm('此操作将增加图书: '+ this.form.bookName +' , 是否继续?', '提示', 
                {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                })
                .then(() => {
                    this.$message({
                        type: 'success',
                        message: '确认增加图书!'
                    });
                    var _this = this                 
                    let data = {
                      bookName: this.form.bookName,
                      bookType: this.form.bookType,
                      author: this.form.author,
                      isBorrowed : false
                    }                    
                    this.$api.post('book/addBook', data)
                        .then((res) => {                        
                            if (res.code === 200) {
                                this.$emit("onReload")
                                this.dialogFormVisible = false
                            } else {
                                this.$message({
                                    type: 'info',
                                    message: '增加用户失败'
                                });   
                            }        
                        })
                })                               
            }
        }
    },    
    mounted(){
      this.getAllBookTypes();
    },
}


</script>
  
  <style scoped>
    .el-icon-circle-plus-outline {
      margin: 50px 0 0 20px;
      font-size: 100px;
      float: left;
      cursor: pointer;
    } 
  </style>